package models

import (
	"encoding/json"
	"time"

	"github.com/go-sql-driver/mysql"
)

type Scene struct {
	ID        int `json:"id"`
	TID       int
	UID       int
	Name      string         `json:"name,omitempty"`
	CreatedAt time.Time      `json:"createdAt"`
	UpdatedAt mysql.NullTime `json:"updatedAt"`
	Descinfo  string         `json:"descinfo"`
}

func (s *Scene) MarshalJSON() ([]byte, error) {
	// TODO: Find a better way to set updatedAt to nil
	if !s.UpdatedAt.Valid {
		return json.Marshal(struct {
			ID        int             `json:"id"`
			Name      string          `json:"name,omitempty"`
			CreatedAt time.Time       `json:"createdAt"`
			UpdatedAt *mysql.NullTime `json:"updatedAt"`
		}{s.ID, s.Name, s.CreatedAt, nil})
	}

	return json.Marshal(struct {
		ID        int       `json:"id"`
		Name      string    `json:"name,omitempty"`
		CreatedAt time.Time `json:"createdAt"`
		UpdatedAt time.Time `json:"updatedAt"`
	}{s.ID, s.Name, s.CreatedAt, s.UpdatedAt.Time})
}
